package com.example.uit_management;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;
/** Theo tác trên sever RegCourse(Đăng kí sinh viên)	 */
public class JsonParser_scheduleID {	
	// Biến Mã sinh viên cần lấy
		private String Id_Student=MainActivity.Student_ID;
		//////////////////////////////////////////
		// Cac Bien trong Json tra ve
		////////////////////////////////////////
		/**  name lich thi*/
		static final String  KEY_name = "name";	
		/** ID cua lich thi theo lop VD:LTCE101.1.1.1  */
		static final String KEY_scheduleID ="scheduleID";
		/** so luon sinh vien toi da cua lop thi  */
		static final String  KEY_scheduleSize = "scheduleSize";	
		/** Ma lop */
		static final String  KEY_courseID = "courseID";	
		/** Ngay thi */
		static final String  KEY_scheduleDay = "scheduleDay";
		/** Phong thi */
		static final String  KEY_roomID = "roomID";	
		/** Ca thi */
		static final String  KEY_scheduleTime = "scheduleTime";	
		/** Thi mon học kì nay */
		static final String  KEY_semesterIndex = "semesterIndex";	
		/** Thi mon nam hoc nay */
		static final String  KEY_semesterYear = "semesterYear";	
		/** Ma mon thi */
		static final String  KEY_subjectID = "subjectID";
		/** Ten mon thi */
		static final String  KEY_subjectName = "subjectName";
		/** Loai lop thi(Thuc hanh hay li thuyet) */
		static final String  KEY_courseType = "courseType";
		/** Mon cua khoa nay */
		static final String  KEY_facultyID = "facultyID";		
		
		//////////////////////////////////////////////////////////
		//Các biến lưu chuổi lấy về cho các Từ khóa trên
		//////////////////////////////////////////////////////////
		/** static name lich thi*/
		static String  name = "name";	
		/**static ID cua lich thi theo lop VD:LTCE101.1.1.1  */
		static String scheduleID ="scheduleID";
		/**static so luon sinh vien toi da cua lop thi  */
		static String  scheduleSize = "scheduleSize";	
		/**static Ma lop */
		static String courseID = "courseID";	
		/**static Ngay thi */
		static String  scheduleDay = "scheduleDay";
		/**static Phong thi */
		static String  roomID = "roomID";	
		/**static Ca thi */
		static String  scheduleTime = "scheduleTime";	
		/**static Thi mon học kì nay */
		static String  semesterIndex = "semesterIndex";	
		/**static Thi mon nam hoc nay */
		static String semesterYear = "semesterYear";	
		/**static Ma mon thi */
		static String subjectID = "subjectID";
		/**static Ten mon thi */
		static String  subjectName = "subjectName";
		/**static Loai lop thi(Thuc hanh hay li thuyet) */
		static String  courseType = "courseType";
		/**static Mon cua khoa nay */
		static String  facultyID = "facultyID";
		//link http://uitstudentmanagement.appspot.com/regCourse?type=bystudent&studentid=08520230
	    // va
		// link 5.	http://uitstudentmanagement.appspot.com/regCourse?type=score&studentid=08520211
		// ke ca mon rot
		

	/** Hàm khởi tạo một JsonParse_regCourse */
	public JsonParser_scheduleID() {
		// TODO Auto-generated constructor stub
	}
	/** Dữ liệu trả về là Array Json ta cần(Lấy theo mã sinh viên)*/
	public JSONArray GetJSONArray_regCourse(){		
		String readTwitterFeed = readTwitterFeed();					
			try
	        {
				// Lấy Json từ sever cung cấp(là Data) Sau đó lấy mảng Array từ Data đó
				JSONObject json = new JSONObject(readTwitterFeed);
				JSONArray jsonArray = json.getJSONArray("data");	
				return jsonArray;
	        }
	        catch (Exception je)
	        {	   
	            Log.e(JsonParser_regCourse.class.toString(), "Method GetJSONArray_regCourse of JsonParser_regCourse");
	        	return null;
	        }	  				
	}
	/** Hàm lấy về Json do Sever cung cấp(o đây là Json là Data)*/
	public String readTwitterFeed() {
		// Các biến cần thiết cho việc lấy JSon từ sever về
	    StringBuilder builder = new StringBuilder();
	    HttpClient client = new DefaultHttpClient();
	    //HttpGet httpGet = new HttpGet("http://uitstudentmanagement.appspot.com/regCourse");
	    HttpGet httpGet =new HttpGet("http://uitstudentmanagement.appspot.com/schedule?type=bystudent&value="+MainActivity.Student_ID);
	    
	    // Lấy dữ liệu về
	    try {
	        HttpResponse response = client.execute(httpGet);
	        StatusLine statusLine = response.getStatusLine();
	        int statusCode = statusLine.getStatusCode();
	        if (statusCode == 200) {
	            HttpEntity entity = response.getEntity();
	            InputStream content = entity.getContent();
	            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
	            String line;
	            while ((line = reader.readLine()) != null) {
	                builder.append(line);
	            }
	        } else {
	            Log.e(JsonParser_regCourse.class.toString(), "Method readTwtterFeed of JsonParser_regCourse statusCode != 200");
	        }
	    } catch (ClientProtocolException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    // Dữ liệu được trả về
	    return builder.toString();
	}	
	/**
	 *Hàm trả về Array list HashMap từ sever regCourse(Các môn SV đã đăng kí)
	 */
	public ArrayList<HashMap<String, String>> getMap()
	{
		// Biến Array list hashMap
		ArrayList<HashMap<String, String>> ArrayMap = new ArrayList<HashMap<String, String>>();
		JSONArray jsonArray = GetJSONArray_regCourse();
		// looping through all jsonarray nodes 
		Log.e(JsonParser_schedule.class.toString(), "Tatol member :"+jsonArray.length());
		for (int i = 0; i < jsonArray.length(); i++) {
			try {
				//create new JsonObject	
				JSONObject js = jsonArray.getJSONObject(i);
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();			
				// adding each child node to HashMap key => value	
			    name		=js.getString("name");
				Log.e(JsonParser_schedule.class.toString(), "name :"+name+"_____Get String score1 JsonParser_schedule");
				scheduleID 		=js.getString("scheduleID");
				Log.e(JsonParser_schedule.class.toString(), "scheduleID:"+scheduleID+"_____Get String score2 JsonParser_schedule");
				scheduleSize	=js.getString("scheduleSize");
				Log.e(JsonParser_schedule.class.toString(), "scheduleSize:"+scheduleSize+"_____Get String score3 JsonParser_schedule");
				courseID		=js.getString("courseID");	
				Log.e(JsonParser_schedule.class.toString(), "courseID:"+courseID+"_____Get String scoreFinal JsonParser_schedule");
				scheduleDay		=js.getString("scheduleDay");
				Log.e(JsonParser_schedule.class.toString(), "scheduleDay:"+scheduleDay+"_____Get String courseID JsonParser_schedule");
				roomID	=js.getString("roomID");
				Log.e(JsonParser_schedule.class.toString(), "roomID:"+roomID+"_____Get String studentID JsonParser_schedule");
				scheduleTime	=js.getString("scheduleTime");			
				Log.e(JsonParser_schedule.class.toString(), "scheduleTime:"+scheduleTime+"_____Get String regCourseID JsonParser_schedule");
				semesterIndex	=js.getString("semesterIndex");
				Log.e(JsonParser_schedule.class.toString(), "semesterIndex:"+semesterIndex+"_____Get String studentName JsonParser_schedule");
				semesterYear=js.getString("semesterYear");
				Log.e(JsonParser_schedule.class.toString(), "semesterYear:"+semesterYear+"_____Get String studentName JsonParser_schedule");
				subjectID=js.getString("subjectID");
				Log.e(JsonParser_schedule.class.toString(), "subjectID:"+subjectID+"_____Get String studentName JsonParser_schedule");
				subjectName=js.getString("subjectName");
				Log.e(JsonParser_schedule.class.toString(), "subjectName:"+subjectName+"_____Get String studentName JsonParser_schedule");
				courseType=js.getString("courseType");
				Log.e(JsonParser_schedule.class.toString(), "courseType:"+courseType+"_____Get String studentName JsonParser_regCourse");
				facultyID=js.getString("facultyID");
				Log.e(JsonParser_schedule.class.toString(), "facultyID:"+facultyID+"_____Get String studentName JsonParser_regCourse");
				
				map.put(KEY_name, name);	
				map.put(KEY_scheduleID, scheduleID);	
				map.put(KEY_scheduleSize, scheduleSize);	
				map.put(KEY_courseID, courseID);	
				map.put(KEY_scheduleDay, scheduleDay);	
				map.put(KEY_roomID, roomID);	
				map.put(KEY_scheduleTime, scheduleTime);	
				map.put(KEY_semesterIndex, semesterIndex);		
				map.put(KEY_semesterYear, semesterYear);	
				map.put(KEY_subjectID, subjectID);			
				map.put(KEY_subjectName, subjectName);	
				map.put(KEY_courseType, courseType);
				map.put(KEY_facultyID, facultyID);
				
				ArrayMap.add(map);					
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
				//Log.e(JsonParser_regCourse.class.toString(), "Exception Method GetMapKey");
			}
			
		}		
		return ArrayMap;
	}	
}
